﻿using System;
using System.Data;
using System.Text;
using MySql.Data.MySqlClient;
using CollegeShop.Api.DBUtility;//Please add references
namespace CollegeShop.Api.Infrastructure
{
	/// <summary>
	/// 数据访问类:t_issue
	/// </summary>
	public partial class t_issue
	{
		public t_issue()
		{}
		#region  BasicMethod

		/// <summary>
		/// 得到最大ID
		/// </summary>
		public int GetMaxId(int college)
		{
            return DbHelperMySQL.GetMaxID(college, "ID", "t_issue"); 
		}

		/// <summary>
		/// 是否存在该记录
		/// </summary>
        public bool Exists(int college, int ID)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) from t_issue");
			strSql.Append(" where ID=@ID");
			MySqlParameter[] parameters = {
					new MySqlParameter("@ID", MySqlDbType.Int32)
			};
			parameters[0].Value = ID;

            return DbHelperMySQL.Exists(college, strSql.ToString(), parameters);
		}


		/// <summary>
		/// 增加一条数据
		/// </summary>
        public bool Add(int college, CollegeShop.Api.Entity.t_issue model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("insert into t_issue(");
			strSql.Append("IssueNumber,SumAmount,SumSaleMoney,SalesUnitID,CreateUser,CreateTime,DeliveryUser,DeliveryPhone,ReceivedUser,ReceivedTime,State,Remark)");
			strSql.Append(" values (");
			strSql.Append("@IssueNumber,@SumAmount,@SumSaleMoney,@SalesUnitID,@CreateUser,@CreateTime,@DeliveryUser,@DeliveryPhone,@ReceivedUser,@ReceivedTime,@State,@Remark)");
			MySqlParameter[] parameters = {
					new MySqlParameter("@IssueNumber", MySqlDbType.VarChar,30),
					new MySqlParameter("@SumAmount", MySqlDbType.Int32,11),
					new MySqlParameter("@SumSaleMoney", MySqlDbType.Decimal,18),
					new MySqlParameter("@SalesUnitID", MySqlDbType.Int32,11),
					new MySqlParameter("@CreateUser", MySqlDbType.VarChar,100),
					new MySqlParameter("@CreateTime", MySqlDbType.DateTime),
					new MySqlParameter("@DeliveryUser", MySqlDbType.VarChar,20),
					new MySqlParameter("@DeliveryPhone", MySqlDbType.VarChar,20),
					new MySqlParameter("@ReceivedUser", MySqlDbType.VarChar,100),
					new MySqlParameter("@ReceivedTime", MySqlDbType.DateTime),
					new MySqlParameter("@State", MySqlDbType.Int32,11),
					new MySqlParameter("@Remark", MySqlDbType.VarChar,100)};
			parameters[0].Value = model.IssueNumber;
			parameters[1].Value = model.SumAmount;
			parameters[2].Value = model.SumSaleMoney;
			parameters[3].Value = model.SalesUnitID;
			parameters[4].Value = model.CreateUser;
			parameters[5].Value = model.CreateTime;
			parameters[6].Value = model.DeliveryUser;
			parameters[7].Value = model.DeliveryPhone;
			parameters[8].Value = model.ReceivedUser;
			parameters[9].Value = model.ReceivedTime;
			parameters[10].Value = model.State;
			parameters[11].Value = model.Remark;

            int rows = DbHelperMySQL.ExecuteSql(college, strSql.ToString(), parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}
		/// <summary>
		/// 更新一条数据
		/// </summary>
        public bool Update(int college, CollegeShop.Api.Entity.t_issue model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("update t_issue set ");
			strSql.Append("IssueNumber=@IssueNumber,");
			strSql.Append("SumAmount=@SumAmount,");
			strSql.Append("SumSaleMoney=@SumSaleMoney,");
			strSql.Append("SalesUnitID=@SalesUnitID,");
			strSql.Append("CreateUser=@CreateUser,");
			strSql.Append("CreateTime=@CreateTime,");
			strSql.Append("DeliveryUser=@DeliveryUser,");
			strSql.Append("DeliveryPhone=@DeliveryPhone,");
			strSql.Append("ReceivedUser=@ReceivedUser,");
			strSql.Append("ReceivedTime=@ReceivedTime,");
			strSql.Append("State=@State,");
			strSql.Append("Remark=@Remark");
			strSql.Append(" where ID=@ID");
			MySqlParameter[] parameters = {
					new MySqlParameter("@IssueNumber", MySqlDbType.VarChar,30),
					new MySqlParameter("@SumAmount", MySqlDbType.Int32,11),
					new MySqlParameter("@SumSaleMoney", MySqlDbType.Decimal,18),
					new MySqlParameter("@SalesUnitID", MySqlDbType.Int32,11),
					new MySqlParameter("@CreateUser", MySqlDbType.VarChar,100),
					new MySqlParameter("@CreateTime", MySqlDbType.DateTime),
					new MySqlParameter("@DeliveryUser", MySqlDbType.VarChar,20),
					new MySqlParameter("@DeliveryPhone", MySqlDbType.VarChar,20),
					new MySqlParameter("@ReceivedUser", MySqlDbType.VarChar,100),
					new MySqlParameter("@ReceivedTime", MySqlDbType.DateTime),
					new MySqlParameter("@State", MySqlDbType.Int32,11),
					new MySqlParameter("@Remark", MySqlDbType.VarChar,100),
					new MySqlParameter("@ID", MySqlDbType.Int32,11)};
			parameters[0].Value = model.IssueNumber;
			parameters[1].Value = model.SumAmount;
			parameters[2].Value = model.SumSaleMoney;
			parameters[3].Value = model.SalesUnitID;
			parameters[4].Value = model.CreateUser;
			parameters[5].Value = model.CreateTime;
			parameters[6].Value = model.DeliveryUser;
			parameters[7].Value = model.DeliveryPhone;
			parameters[8].Value = model.ReceivedUser;
			parameters[9].Value = model.ReceivedTime;
			parameters[10].Value = model.State;
			parameters[11].Value = model.Remark;
			parameters[12].Value = model.ID;

            int rows = DbHelperMySQL.ExecuteSql(college, strSql.ToString(), parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}

		/// <summary>
		/// 删除一条数据
		/// </summary>
        public bool Delete(int college, int ID)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from t_issue ");
			strSql.Append(" where ID=@ID");
			MySqlParameter[] parameters = {
					new MySqlParameter("@ID", MySqlDbType.Int32)
			};
			parameters[0].Value = ID;

            int rows = DbHelperMySQL.ExecuteSql(college, strSql.ToString(), parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}
		/// <summary>
		/// 批量删除数据
		/// </summary>
        public bool DeleteList(int college, string IDlist)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from t_issue ");
			strSql.Append(" where ID in ("+IDlist + ")  ");
            int rows = DbHelperMySQL.ExecuteSql(college, strSql.ToString());
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}


		/// <summary>
		/// 得到一个对象实体
		/// </summary>
        public CollegeShop.Api.Entity.t_issue GetModel(int college, int ID)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select ID,IssueNumber,SumAmount,SumSaleMoney,SalesUnitID,CreateUser,CreateTime,DeliveryUser,DeliveryPhone,ReceivedUser,ReceivedTime,State,Remark from t_issue ");
			strSql.Append(" where ID=@ID");
			MySqlParameter[] parameters = {
					new MySqlParameter("@ID", MySqlDbType.Int32)
			};
			parameters[0].Value = ID;

			CollegeShop.Api.Entity.t_issue model=new CollegeShop.Api.Entity.t_issue();
            DataSet ds = DbHelperMySQL.Query(college, strSql.ToString(), parameters);
			if(ds.Tables[0].Rows.Count>0)
			{
				return DataRowToModel(ds.Tables[0].Rows[0]);
			}
			else
			{
				return null;
			}
		}


		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public CollegeShop.Api.Entity.t_issue DataRowToModel(DataRow row)
		{
			CollegeShop.Api.Entity.t_issue model=new CollegeShop.Api.Entity.t_issue();
			if (row != null)
			{
				if(row["ID"]!=null && row["ID"].ToString()!="")
				{
					model.ID=int.Parse(row["ID"].ToString());
				}
				if(row["IssueNumber"]!=null)
				{
					model.IssueNumber=row["IssueNumber"].ToString();
				}
				if(row["SumAmount"]!=null && row["SumAmount"].ToString()!="")
				{
					model.SumAmount=int.Parse(row["SumAmount"].ToString());
				}
				if(row["SumSaleMoney"]!=null && row["SumSaleMoney"].ToString()!="")
				{
					model.SumSaleMoney=decimal.Parse(row["SumSaleMoney"].ToString());
				}
				if(row["SalesUnitID"]!=null && row["SalesUnitID"].ToString()!="")
				{
					model.SalesUnitID=int.Parse(row["SalesUnitID"].ToString());
				}
				if(row["CreateUser"]!=null)
				{
					model.CreateUser=row["CreateUser"].ToString();
				}
				if(row["CreateTime"]!=null && row["CreateTime"].ToString()!="")
				{
					model.CreateTime=DateTime.Parse(row["CreateTime"].ToString());
				}
				if(row["DeliveryUser"]!=null)
				{
					model.DeliveryUser=row["DeliveryUser"].ToString();
				}
				if(row["DeliveryPhone"]!=null)
				{
					model.DeliveryPhone=row["DeliveryPhone"].ToString();
				}
				if(row["ReceivedUser"]!=null)
				{
					model.ReceivedUser=row["ReceivedUser"].ToString();
				}
				if(row["ReceivedTime"]!=null && row["ReceivedTime"].ToString()!="")
				{
					model.ReceivedTime=DateTime.Parse(row["ReceivedTime"].ToString());
				}
				if(row["State"]!=null && row["State"].ToString()!="")
				{
					model.State=int.Parse(row["State"].ToString());
				}
				if(row["Remark"]!=null)
				{
					model.Remark=row["Remark"].ToString();
				}
			}
			return model;
		}

		/// <summary>
		/// 获得数据列表
		/// </summary>
        public DataSet GetList(int college, string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select ID,IssueNumber,SumAmount,SumSaleMoney,SalesUnitID,CreateUser,CreateTime,DeliveryUser,DeliveryPhone,ReceivedUser,ReceivedTime,State,Remark ");
			strSql.Append(" FROM t_issue ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
            return DbHelperMySQL.Query(college, strSql.ToString());
		}

		/// <summary>
		/// 获取记录总数
		/// </summary>
        public int GetRecordCount(int college, string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) FROM t_issue ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
            object obj = DbHelperMySQL.GetSingle(college, strSql.ToString());
			if (obj == null)
			{
				return 0;
			}
			else
			{
				return Convert.ToInt32(obj);
			}
		}
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
        public DataSet GetListByPage(int college, string strWhere, string orderby, int startIndex, int endIndex)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("SELECT * FROM ( ");
			strSql.Append(" SELECT ROW_NUMBER() OVER (");
			if (!string.IsNullOrEmpty(orderby.Trim()))
			{
				strSql.Append("order by T." + orderby );
			}
			else
			{
				strSql.Append("order by T.ID desc");
			}
			strSql.Append(")AS Row, T.*  from t_issue T ");
			if (!string.IsNullOrEmpty(strWhere.Trim()))
			{
				strSql.Append(" WHERE " + strWhere);
			}
			strSql.Append(" ) TT");
			strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
            return DbHelperMySQL.Query(college, strSql.ToString());
		}

		/*
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetList(int PageSize,int PageIndex,string strWhere)
		{
			MySqlParameter[] parameters = {
					new MySqlParameter("@tblName", MySqlDbType.VarChar, 255),
					new MySqlParameter("@fldName", MySqlDbType.VarChar, 255),
					new MySqlParameter("@PageSize", MySqlDbType.Int32),
					new MySqlParameter("@PageIndex", MySqlDbType.Int32),
					new MySqlParameter("@IsReCount", MySqlDbType.Bit),
					new MySqlParameter("@OrderType", MySqlDbType.Bit),
					new MySqlParameter("@strWhere", MySqlDbType.VarChar,1000),
					};
			parameters[0].Value = "t_issue";
			parameters[1].Value = "ID";
			parameters[2].Value = PageSize;
			parameters[3].Value = PageIndex;
			parameters[4].Value = 0;
			parameters[5].Value = 0;
			parameters[6].Value = strWhere;	
			return DbHelperMySQL.RunProcedure("UP_GetRecordByPage",parameters,"ds");
		}*/

		#endregion  BasicMethod
		#region  ExtensionMethod

		#endregion  ExtensionMethod
	}
}

